package com.example.springbootshiro.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Created by YangGang on 2021/12/15 11:03。
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

    //通过bean名称获取bean对象
    public static Object getBeanByBeanName(String beanName) {
        return context.getBean(beanName);
    }

    //通过class获取bean对象
    public static <T> T getBeanByClass(Class<T> clazz) {
        return context.getBean(clazz);
    }

}
